#! /bin/bash
# hdfs_to_ods_db.sh all/表名 [日期]
#1、判断参数是否传入
if [ $# -lt 1 ]
then
	echo "必须至少传入all/表名一个参数..."
	exit
fi
#2、判断日期是否传入,如果有日期用指定日期,如果没有日期则用前一天日期
[ "$2" ] && datestr=$2 || datestr=$(date -d '-1 day' +%F)

function loadData(){
	sql="use edu;"
	for tableName in $*
	do
		path="/edu_origin_data/edu/db/${tableName:4}/$datestr"
		#判断目录是否存在,目录存在才拼接sql语句
		hdfs dfs -test -e $path
		if [ $? == 0 ]
		then
			echo "${tableName:4}表${datestr}日期的目录存在,拼接sql中...."
			sql="$sql;load data inpath '$path' overwrite into table ${tableName} partition (dt='$datestr')"
		else
			echo "-------------${tableName:4}表${datestr}日期的目录不存在-------------"
		fi
	done
	/opt/module/hive/bin/hive -e "$sql"
}
#3、根据第一个参数匹配加载数据到ODS
case $1 in
"all")
	echo "正在加载表"
	loadData "ods_test_paper_full" "ods_order_info_inc" " ods_base_category_info_full" "ods_base_province_full" "ods_base_source_inc" "ods_base_subject_info_full" "ods_cart_info_inc" "ods_chapter_info_full" "ods_comment_info_inc" "ods_course_info_full" "ods_favor_info_inc" "ods_knowledge_point_full" "ods_order_detail_inc" "ods_payment_info_inc"  "ods_review_info_inc" "ods_test_exam_full" "ods_test_exam_inc" "ods_test_exam_question_inc" "ods_test_paper_question_full" "ods_test_point_question_full" "ods_test_question_info_full" "ods_test_question_option_full" "ods_user_chapter_process_inc" "ods_user_info_inc" "ods_video_info_full" "ods_vip_change_detail_inc"
;;
"ods_base_category_info_full")
	echo "正在加载表ods_base_category_info_full"
	loadData "ods_base_category_info_full"
;;
"ods_test_paper_full")
	echo "正在加载表ods_test_paper_full"
	loadData "ods_test_paper_full"
;;
"ods_order_info_inc")
	echo "正在加载表ods_order_info_inc"
	loadData "ods_order_info_inc"
;;
"ods_base_province_full")
	echo "正在加载表ods_base_province_full"
	loadData "ods_base_province_full"
;;
"ods_base_source_inc")
	echo "正在加载表ods_base_source_inc"
	loadData "ods_base_source_inc"
;;
"ods_base_subject_info_full")
	echo "正在加载表ods_base_subject_info_full"
	loadData "ods_base_subject_info_full"
;;
"ods_cart_info_inc")
	echo "正在加载表ods_cart_info_inc"
	loadData "ods_cart_info_inc"
;;
"ods_chapter_info_full")
	echo "正在加载表ods_chapter_info_full"
	loadData "ods_chapter_info_full"
;;
"ods_comment_info_inc")
	echo "正在加载表ods_comment_info_inc"
	loadData "ods_comment_info_inc"
;;
"ods_course_info_full")
	echo "正在加载表ods_course_info_full"
	loadData "ods_course_info_full"
;;
"ods_favor_info_inc")
	echo "正在加载表ods_favor_info_inc"
	loadData "ods_favor_info_inc"
;;
"ods_knowledge_point_full")
	echo "正在加载表ods_knowledge_point_full"
	loadData "ods_knowledge_point_full"
;;
"ods_order_detail_inc")
	echo "正在加载表ods_order_detail_inc"
	loadData "ods_order_detail_inc"
;;
"ods_payment_info_inc")
	echo "正在加载表ods_payment_info_inc"
	loadData "ods_payment_info_inc"
;;
"ods_review_info_inc")
	echo "正在加载表ods_review_info_inc"
	loadData "ods_review_info_inc"
;;
"ods_test_exam_full")
	echo "正在加载表ods_test_exam_full"
	loadData "ods_test_exam_full"
;;
"ods_test_exam_inc")
	echo "正在加载表ods_test_exam_inc"
	loadData "ods_test_exam_inc"
;;
"ods_test_exam_question_inc")
	echo "正在加载表ods_test_exam_question_inc"
	loadData "ods_test_exam_question_inc"
;;
"ods_test_paper_question_full")
	echo "正在加载表ods_test_paper_question_full"
	loadData "ods_test_paper_question_full"
;;
"ods_test_point_question_full")
	echo "正在加载表ods_test_point_question_full"
	loadData "ods_test_point_question_full"
;;
"ods_test_question_info_full")
	echo "正在加载表ods_test_question_info_full"
	loadData "ods_test_question_info_full"
;;
"ods_test_question_option_full")
	echo "正在加载表ods_test_question_option_full"
	loadData "ods_test_question_option_full"
;;
"ods_user_chapter_process_inc")
	echo "正在加载表ods_user_chapter_process_inc"
	loadData "ods_user_chapter_process_inc"
;;
"ods_user_info_inc")
	echo "正在加载表ods_user_info_inc"
	loadData "ods_user_info_inc"
;;
"ods_video_info_full")
	echo "正在加载表ods_video_info_full"
	loadData "ods_video_info_full"
;;
"ods_vip_change_detail_inc")
	echo "正在加载表ods_vip_change_detail_inc"
	loadData "ods_vip_change_detail_inc"
;;
*)
	echo "参数输入错误,必须是all/表名"
;;
esac